package com.atom.wyz.new_opengl.util;

import java.util.ArrayList;
import java.util.Collections;

public final class TerrainCreateUtil {

    public static void createTerrain(
            float[][] result,
            int cx,
            int cy,
            int count,
            int span,
            int gdyzIn,
            boolean ssfxjh,
            boolean sfSmms,
            int[][] smwz
    ) {

        @SuppressWarnings("rawtypes")
        ArrayList[] knPosition = new ArrayList[span];
        for (int k = 1; k <= span; k++)
        {
            knPosition[k - 1] = new ArrayList<int[]>();
            int powerSpan = k * k;
            for (int i = -k; i <= k; i++)
            {
                for (int j = -k; j <= k; j++) {
                    if (i == 0 && j == 0) continue;
                    if (i * i + j * j <= powerSpan)
                    {
                        knPosition[k - 1].add(new int[]{i, j});
                    }
                }
            }
        }
        int width = result.length;
        int height = result[0].length;
        for (int i = 0; i < count; i++) {

            int currX = cx;
            int currY = cy;
            if (sfSmms) {
                currX = smwz[i % smwz.length][0];
                currY = smwz[i % smwz.length][1];
            }


            zong:
            while (true) {
                float currHeight = result[currX][currY];
                int currSpan = (int) Math.ceil(span * Math.random());
                int gdyz = (int) (Math.ceil((gdyzIn / 2.0) * Math.random() + (gdyzIn / 2.0)));
                ArrayList<int[]> knwz = (ArrayList<int[]>) (knPosition[currSpan - 1]);

                if (ssfxjh) {
                    Collections.shuffle(knwz);
                }

                for (int[] wz : knwz) {
                    int j = wz[0];
                    int k = wz[1];

                    if (currX + j < 0 || currX + j >= width || currY + k < 0 || currY + k >= height) {
                        continue;
                    }

                    float tempHeight = result[currX + j][currY + k];

                    if (currHeight - tempHeight > gdyz) {

                        currX = currX + j;
                        currY = currY + k;

                        continue zong;
                    }
                }

                break zong;
            }
            result[currX][currY] += 1;
        }
    }

}
